package com.qfedu.zf.app.config;


import com.qfedu.zf.common.config.SystemConfig;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Date: 2023/10/10 14:23
 *
 * RequestInterceptor 请求拦截器
 *   作用就是拦截客户端的请求，将请求中的header 读取出来，别配置到 feign 发送远程请求的header
 */

public class OpenFeignConfig implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //1.获取请求对象
        // RequestContextHolder 作用和 ThreadLocal类似，就是获取当前线程的副本request
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request=attributes.getRequest();

        //2.把需要传递的请求消息头传输给OpenFeign
        // RequestTemplate 就是 feign的核心实现
        // 此时完成 将请求header中的token 配置到 RequestTemplate
        template.header(SystemConfig.HEADER_TOKEN,request.getHeader(SystemConfig.HEADER_TOKEN));


    }
}